<?php
class IntegralsAction extends CommonAction {
	
	
	
	
	public function delete(){
		$this->dao = M('Integral_pay');
		parent::delete();
	}
	
	
	
	
	
	
	
	
	function index(){
		
	   $integraltype = M('integral_type')->where(array('site_id'=>$this->siteid))->select();
	   $this->assign('list',$integraltype);
	   $this->display();
	
	}
	
	
	function add(){
	      if(IS_GET){
			$integral_typeDB = M('integral_type');
			if($this->_get('type') == 'delete'){
			  $mes = $integral_typeDB->where(array('id'=>$this->_get('id')))->delete();
			  if($mes){
				  $this->success('操作成功',U('index'));
				  exit; 
				}else{
				  $this->error('操作失败');
				  exit;
				}
			}
		  }
		  if(IS_POST){
			$integral_typeDB = M('integral_type');
			$data['remark']=$this->_post('remark');
			$data['typename']=$this->_post('typename');
			$data['typeratio']=$this->_post('typeratio');
			$data['site_id']=$this->siteid;
            if(''!=$_POST['aimtype']&&''!=$_POST['aimratio']){
                $data['aimratio']=serialize(array($_POST['aimtype']=>$_POST['aimratio']));    
            }
			$integral_typeDB = M('integral_type');
			$integral_type = $integral_typeDB->where(array('typename'=>$data['typename'],'site_id'=>$data['site_id']))->find();
			if($integral_type){
				$this->error('已存在');
			  exit;
			}
			$mes = $integral_typeDB->add($data);
			if($mes){
			  $this->success('新增成功',U('index'));
			  exit; 
			}else{
			  $this->error('添加失败');
			  exit;
			}
		  }
		  $Integrals = C('Integrals');
		  $this->assign('Integrals',$Integrals);
	      $this->display();
	}
	
	
  public function integralgoods(){
     
		    $pagesize = 10;
		 
			if($this->_get('pagesize')){
				
			 $pagesize = $this->_get('pagesize');
			
			}
			$map = array(
			
			  'p.site_id' => $this->siteid,
			
			);
			if($this->_get('keywords')){
				
				$map['o.ordernumber'] = $this->_get('keywords');
			}
			
			 
			if($this->_get('stime') || $this->_get('etime')){
			  
			  $stime = strtotime($this->_get('stime'))+8*3600;
			  $etime = strtotime($this->_get('etime'))+8*3600;
			  
			 if($this->_get('stime')){
				 
				 $map['o.create_time'] = array('gt',$stime);
			  
			 }
			 
			 if($this->_get('etime')){
				 
			  $map['o.create_time'] = array('lt',$etime);
			 
			 }
			 
			 if($this->_get('etime') && $this->_get('stime')){
				 
			  $map['o.create_time'] = array('gt',$stime);
			  $map['o.create_time'] = array('lt',$etime);
			 
			 }
			 
			}
	    
		   $integral_payDB = D('integral_pay');
		   $count = $integral_payDB-> alias('i')->join('INNER JOIN __PRODUCT__ as p on p.id = i.product_id')->where($map)->getField('count(distinct(i.id)) as tp');
		   //实例化分页类
			import ( 'ORG.Util.Page' );
			$p=new Page($count,$pagesize);
			
			$data = $integral_payDB->alias('i')->join('LEFT JOIN __PRODUCT__ as p on p.id = i.product_id')->where($map)->order('i.id DESC')->group("product_id")->limit($p->firstRow.',
	'.$p->listRows) -> Field('p.title, p.img_url, p.price, i.*') -> select();
			
		   $pages=$p->show();
		   $this->assign('list',$data);
		   $this->assign('pages',$pages);
		   $this->display();
  }
  
  
   public function editIntegralgoods(){
	   
	   $integral_payDB = M('integral_pay');
      
	    if(IS_POST){
		   
		   $data['product_id'] = $this->_post('product_id');
		   $data['intergralnum'] = $this->_post('intergralnum');
		   $data['degree'] = $this->_post('degree');
		   
		   
		   if($this->_post('product_id') && $this->_post('intergralnum') && $this->_post('degree')){
			   
		      if($this->_post('id')){
				  
			   $mes = $integral_payDB->where(array('id'=>$this->_post('id')))->save($data);
			  
			  }else{
				  
			   $mes = $integral_payDB->add($data);
			  
			  }
			  if($mes){
				  
			     $this->success("添加成功",U('index'));
				 
			  }else{
				  
			     $this->error("参数错误1");
			  }
		   
		   }else{
			   
		      $this->error("参数错误2");
		   }
		   
		   
		   exit;
		
		}
		
		if($this->_get('id')){
		$info = $integral_payDB->where(array('id'=>$this->_get('id')))->find();
		$this->assign('info',$info);
		}
        $this->display();
   }
	
	
}

?>